


#include "I2c_lkt4106.h"
#include "xrj_des.h"
#include "includes.h"

St_LKT_BUF PassWord_LKT;
#define GPIO_LKT4106_RESET       GPIO_Pin_5
#define	P_Out_LKT4106_RESET	 	 PBout(5)


void LKT4106_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); //原来的初始化代码没有打开时钟 

	/* Configure I2C1 pins: SCL and SDA */ 
	GPIO_InitStructure.GPIO_Pin =  GPIO_LKT4106_RESET; 
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;      // 开漏   
	GPIO_Init(GPIOB, &GPIO_InitStructure); 
	
	P_Out_LKT4106_RESET = 0;
	
	MIIC_Init();
}



void LKT4106_PIN_Reset(void)
{
	P_Out_LKT4106_RESET = 1;
    P_Out_LKT4106_RESET = 0;
    OSTimeDlyHMSM(0,0,0,50);
	P_Out_LKT4106_RESET = 1;
    OSTimeDlyHMSM(0,0,0,500);
}



INT32 LKT4106_I2C_Write(UINT8* pBuffer,UINT32 pBuffer_len)
{
	INT32 i = 0;
	
	MIIC_Start();
	MIIC_Write_Byte(LKT4106_ADDRESS);
	if (MIIC_WaitAck() == FALSE)
	{
		return 0;
	}	
	for (i = 0; i < pBuffer_len; i++)
	{
		MIIC_Write_Byte(pBuffer[i]);
		if (MIIC_WaitAck() == FALSE)
		{
			return 0;
		}
	}
	MIIC_Stop();
	return 1;
}

INT32 LKT4106_I2C_Read(UINT8* pBuffer,UINT32 pBuffer_len)
{
	MIIC_Start();
	MIIC_Write_Byte(LKT4106_ADDRESS+1);
	if (MIIC_WaitAck() == FALSE)
	{
		return 0;
	}
	while(pBuffer_len)
    {
		*pBuffer = MIIC_Read_Byte();	
		pBuffer++;
        pBuffer_len--;
		if(pBuffer_len == 0)
        {             
			MIIC_NoAck();
        }
		else
		{
			MIIC_Ack();
		}	
	
	}
	MIIC_Stop();
	return 1;
}



void HexDump(UINT8 *buf,INT32 len,INT32 addr)
{
    int i,j,k;
    char binstr[80];

    for (i=0;i<len;i++) 
	{
        if (0==(i%16)) 
		{
            sprintf(binstr,"%08x -",i+addr);
            sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
        } 
		else if (15==(i%16)) 
		{
            sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
            sprintf(binstr,"%s  ",binstr);
            for (j=i-15;j<=i;j++) 
			{
                sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
            }
            printf("%s\n",binstr);
        }
		else 
		{
            sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
        }
    }
    if (0!=(i%16)) 
	{
        k=16-(i%16);
        for (j=0;j<k;j++) 
		{
            sprintf(binstr,"%s   ",binstr);
        }
        sprintf(binstr,"%s  ",binstr);
        k=16-k;
        for (j=i-k;j<i;j++) 
		{
            sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
        }
        printf("%s\n",binstr);
    }
}






 unsigned char lkt4106_i2c_sendbuf[256];	
 unsigned char lkt4106_i2c_recebuf[256];	

const unsigned char default_key[16] = {0xFF,0XEE,0XDD,0XCC,0XBB,0XAA,0X99,0X88,0X77,0X66,0X55,0X44,0X33,0X22,0X11,0x00};

const unsigned char Result_key[16] ={0x00,0x11,0x22 ,0x33 ,0x44 ,0x55 ,0x66 ,0x77 ,0x88 ,0x99 ,0xaa ,0xbb ,0xcc ,0xdd ,0xee ,0xff};

INT32 Get_Net_Key(UINT8* net_key)
{
    u8 CmdLen = 0;
    u16 PackLen = 0;
    int res;
    u16 RecvLen = 0;
    u8 D1[16];
    u8 D2[16];
    u8 D3[16];
    
    PackLen+=2;
    
    memcpy(lkt4106_i2c_sendbuf+PackLen,LKT4106_CMD_HEAD,4);

    PackLen+=4;

    CmdLen = 1;

    lkt4106_i2c_sendbuf[PackLen] = CmdLen;

    PackLen+=1;

    lkt4106_i2c_sendbuf[PackLen] = LKT4106_CMD_GET_RANDOM;

    PackLen+=1;

    lkt4106_i2c_sendbuf[0] = (PackLen-2)/256;
    lkt4106_i2c_sendbuf[1] = (PackLen-2)%256;

    printf("lkt4106_i2c_sendbuf:\r\n");
    HexDump(lkt4106_i2c_sendbuf,PackLen,(int)lkt4106_i2c_sendbuf);
    
    res = LKT4106_I2C_Write(lkt4106_i2c_sendbuf,PackLen);
    if(res == 0)
    {
        printf("LKT4106_I2C_Write fail !\r\n");
        return -1;
    }

    OSTimeDlyHMSM(0,0,0,500);

    res = LKT4106_I2C_Read(lkt4106_i2c_recebuf,22);
    if(res == 0)
    {
        printf("LKT4106_I2C_Read fail !\r\n");
        return -1;
    }

    
    RecvLen = lkt4106_i2c_recebuf[0]<<8|lkt4106_i2c_recebuf[1];
	
	if (RecvLen > 22)
	{
		return -1;
	}	
    printf("lkt4106_i2c_recebuf:\r\n");
    HexDump(lkt4106_i2c_recebuf,RecvLen+2,(int)lkt4106_i2c_recebuf);


    if(memcmp(&lkt4106_i2c_recebuf[RecvLen],"\x90\x00",2) != 0)
    {
        printf("lkt4106 deal fail\r\n");       
        return -2;
    }


    memcpy(D1,lkt4106_i2c_recebuf+2,RecvLen-2);

    printf("D1 :\r\n");
    HexDump(D1,16,(int)D1);

    Des_encrypt(16,16,(u8 *)default_key,D1,D2);

    printf("D2 :\r\n");
    HexDump(D2,16,(int)D2);

    PackLen = 0;

    PackLen+=2;
   
   memcpy(lkt4106_i2c_sendbuf+PackLen,LKT4106_CMD_HEAD,4);

   PackLen+=4;

   CmdLen = 1;

   lkt4106_i2c_sendbuf[PackLen] = CmdLen;

   PackLen+=1;

   lkt4106_i2c_sendbuf[PackLen] = LKT4106_CMD_GET_EN_NET_KEY;

   PackLen+=1;

   lkt4106_i2c_sendbuf[0] = (PackLen-2)/256;
   lkt4106_i2c_sendbuf[1] = (PackLen-2)%256;

   printf("lkt4106_i2c_sendbuf:\r\n");
   HexDump(lkt4106_i2c_sendbuf,PackLen,(int)lkt4106_i2c_sendbuf);
   
   res = LKT4106_I2C_Write(lkt4106_i2c_sendbuf,PackLen);
   if(res == 0)
   {
       printf("LKT4106_I2C_Write fail !\r\n");
       return -1;
   }

   OSTimeDlyHMSM(0,0,0,500);

   res = LKT4106_I2C_Read(lkt4106_i2c_recebuf,22);
   if(res == 0)
   {
       printf("LKT4106_I2C_Read fail !\r\n");
       return -1;
   }

   
   RecvLen = lkt4106_i2c_recebuf[0]<<8|lkt4106_i2c_recebuf[1];


   printf("lkt4106_i2c_recebuf:\r\n");
   HexDump(lkt4106_i2c_recebuf,RecvLen+2,(int)lkt4106_i2c_recebuf);


   if(memcmp(&lkt4106_i2c_recebuf[RecvLen],"\x90\x00",2) != 0)
   {
       printf("lkt4106 deal fail\r\n");
       return -2;
   }

    
    memcpy(D3,lkt4106_i2c_recebuf+2,RecvLen-2);

    printf("D3 :\r\n");
    HexDump(D3,16,(int)D3);



    Des_decrypt(16,16,D2,D3,net_key);

    printf("net key:\r\n");
    HexDump(net_key,16,(int)net_key);
    
    if (memcmp(net_key,Result_key,16) == 0)
	{
		return 0;
	}	
   
    return -1;
}









